/**
 * 
 */
package vue.popup;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import model.solver.ImportESolver;
import controller.IController;

/**
 * A dialog to remove an imported solver.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGRemoveImportSolverDialog extends JDialog implements ActionListener, KeyListener {
	private static final long	serialVersionUID	= 1L;
	public static final String	TITLE				= "Remove solver dialog";
	public static final String	RUN_MESSAGE			= "REMOVE";
	public static final String	COMBO_TITLE			= "Choose solver";
	// public static final int PARAMETER_DIALOG_POSITION = 1;

	private IController			controller;

	private Set<ImportESolver>	solvers;

	private JComboBox			comboBox;
	private JButton				run;
	private JPanel				panel;

	/**
	 * Create a {@link IGRemoveImportSolverDialog}.
	 * 
	 * @param controller
	 *            the {@link IController} of the application
	 * @param owner
	 *            window owner of the dialog
	 */
	public IGRemoveImportSolverDialog(IController controller, Window owner) {
		super(owner, TITLE);
		this.controller = controller;
		solvers = controller.getImportedSolver();

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		setContentPane(panel);

		// comboBox
		comboBox = new JComboBox(solvers.toArray());
		comboBox.setBorder(BorderFactory.createTitledBorder(COMBO_TITLE));
		comboBox.addActionListener(this);

		panel.add(comboBox);

		// bouton
		run = new JButton(RUN_MESSAGE);
		// run.setEnabled(false);
		run.setAlignmentX(JButton.RIGHT_ALIGNMENT);
		run.addActionListener(this);

		panel.add(run);

		addKeyListener(this);
		setFocusable(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == run)
		{
			controller.removeAvailableSolver((ImportESolver) comboBox.getSelectedItem());
			dispose();
			return;
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
			run.doClick();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// rien
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// rien
	}


}
